//
//  SubOrderViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-23.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SubOrderViewController.h"
#import "Order.h"
#import "GoodDetailWebViewController.h"

#define kJIAN_GE        4

@interface SubOrderViewController()  

- (void)viewGoodDetail;

@end

@implementation SubOrderViewController

@synthesize orderForTableViewDataSource           =           _orderForTableViewDataSource;

- (void)dealloc 
{
    [_tableView release];
    LF_RELEASE_SAFELY(_orderForTableViewDataSource);
    
    [super dealloc];
}

- (id)initWithOrder:(Order *)order_
{
    self = [super init];
    if (self) 
    {
        self.orderForTableViewDataSource = order_;
    }
    
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"子订单";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"查看商品详情" 
                                                                               style: UIBarButtonItemStylePlain
                                                                              target: self 
                                                                              action: @selector(viewGoodDetail)] autorelease];
}

- (void)viewDidUnload
{
    [_tableView release];
    _tableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



#pragma mark - 自定义函数
- (void)viewGoodDetail
{
    GoodDetailWebViewController *goodDetailWebViewController = [[[GoodDetailWebViewController alloc] initWithNum_iid: _orderForTableViewDataSource.num_iid] autorelease];
    
    [self.navigationController pushViewController: goodDetailWebViewController animated: YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"signCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) 
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"SubOrderCell" owner: nil options: nil];
        cell = (UITableViewCell *)[cellArray objectAtIndex: 0];
    }
    UILabel *label = nil;
    
    label = (UILabel *)[cell viewWithTag: 101];
    //label.backgroundColor = [UIColor redColor];
    NSString *title = _orderForTableViewDataSource.title;
    label.text = title;
    CGSize size1 = bubbleView(title, label.font, label.frame.size.width);
    
    float height = size1.height - label.frame.size.height;
    
    label.frame = CGRectMake(label.frame.origin.x,
                             label.frame.origin.y, 
                             label.frame.size.width,
                             size1.height);
    
    
    label = (UILabel *)[cell viewWithTag: 102];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height);
    
    label = (UILabel *)[cell viewWithTag: 103];
    //label.backgroundColor = [UIColor redColor];
    label.text = _orderForTableViewDataSource.status;
    CGSize size2 = bubbleView(label.text, label.font, label.frame.size.width);
    float height1 = size2.height - label.frame.size.height + height;
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y + height, label.frame.size.width, size2.height);
    
    
    label = (UILabel *)[cell viewWithTag: 104];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 105];
    //label.backgroundColor = [UIColor redColor];
    label.text = _orderForTableViewDataSource.price;
    setCellLabelFrame(&label, height1);
    
    
    label = (UILabel *)[cell viewWithTag: 106];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 107];
    //label.backgroundColor = [UIColor redColor];
    label.text = _orderForTableViewDataSource.num;
    setCellLabelFrame(&label, height1);
    
    
    label = (UILabel *)[cell viewWithTag: 108];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 109];
    //label.backgroundColor = [UIColor redColor];
    label.text = _orderForTableViewDataSource.total_fee;
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 110];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 111];
    //label.backgroundColor = [UIColor redColor];
    label.text = _orderForTableViewDataSource.post_fee;
    setCellLabelFrame(&label, height1);
    
    
    label = (UILabel *)[cell viewWithTag: 112];
    //label.backgroundColor = [UIColor redColor];
    setCellLabelFrame(&label, height1);
    
    label = (UILabel *)[cell viewWithTag: 113];
    //label.backgroundColor = [UIColor redColor];
    label.text = [_orderForTableViewDataSource.payment stringByAppendingString: @"(不含运费)"];
    setCellLabelFrame(&label, height1);
    
    
    
    
    return cell;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"SubOrderCell" owner: nil options: nil];
    UITableViewCell *cell = (UITableViewCell *)[cellArray objectAtIndex: 0];
    UILabel *label = nil;
    
    label = (UILabel *)[cell viewWithTag: 101];
    //label.backgroundColor = [UIColor redColor];
    NSString *title = _orderForTableViewDataSource.title;
    CGSize size1 = bubbleView(title, label.font, label.frame.size.width);
    
    float height = size1.height - label.frame.size.height;
    
    label = (UILabel *)[cell viewWithTag: 103];
    NSString *status = _orderForTableViewDataSource.status;
    CGSize size2 = bubbleView(status, label.font, label.frame.size.width);
    float height1 = size2.height - label.frame.size.height + height;
    
    return cell.frame.size.height + height1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize: 14];
    button.titleLabel.textAlignment = UITextAlignmentLeft;
    [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
    
    UIImage *image = nil;
    if (0 == section)
    {
        image = [UIImage imageNamed: @"cell_section_header_view_top.png"];
        
    }else
    {
        image = [UIImage imageNamed: @"cell_section_header_view_middle.png"];
    }
    
    [button setBackgroundImage: image forState: UIControlStateNormal];
    
    [button setTitle: @"子订单详情" forState: UIControlStateNormal];
    
    return button;
}




@end
